<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends Action {
    public function index(){
    	if(!empty($_SESSION["username"])){
    		$this->display();
    	}else{
    		$this->redirect('Index/login/');
    	}
    }
    public function login(){
 	$this->display();
    }
    public function main(){
    $this->display();
    }
    public function loguot()
    {
    	unset($_SESSION["username"]);
    	$this->redirect('Index/login/');
    }
    
    public function submitLogin()
    {
    	$username=$_POST['username'];
    	$password=$_POST['password'];
    	$admin=M('admin');
    	$where['username']=$username;
    	$where['password']=$password;
    	$rs=$admin->where($where)->select();
    	if($rs)
    	{
    		$_SESSION["username"]=$username;
    		$this->display('Index:index');
    	}else{
    		
    		$this->display('Index:login');
    	}
    }
    
    public function addwangba()
    {
    	$this->display();
    }
    
    public function submitWangba()
    {
 		$WbarModel=D('Wbar');
 		if($_POST['id']){
 			$id=$_POST['id'];
 			$data['address']=$_POST['address'];
 			$data['name']=$_POST['name'];
 			$data['boss_name']=$_POST['boss_name'];
 			$data['phone']=$_POST['phone'];
 			$data['password']=$_POST['password'];
 			$rs=$WbarModel->where("id=$id")->save($data);
 			$this->redirect('/Index/wangbaList');
 		}else{
	    if (!$WbarModel->create()){
			exit($WbarModel->getError());
		}else{
			$WbarModel->add();
		  	 $this->redirect('/Index/wangbaList');
		}
 		}
    }
    
	public function wangbaList()
	{
		$WbarModel=D('Wbar');
		$wlist=$WbarModel->select();
		$this->assign('wlist',$wlist);
		$this->display();
	}
	
	public function delWangba()
	{
		 $id = I('get.id', '', 'htmlspecialchars');
		 $Wbar=M('wbar');
		 $rs=$Wbar->where("id=$id")->delete();
		 if($rs!=false)
		 {
		 	$this->redirect('/Index/wangbaList');
		 }
	}
	
	public function editWangba()
	{
		$id = I('get.id', '', 'htmlspecialchars');
		$Wbar=M('wbar');
		$rs=$Wbar->where("id=$id")->select();
		$rs=$rs[0];
		$this->assign('wbar',$rs);
		$this->display();
	}
	public function storeList()
	{
		$StoreModel=M('store');
		$storeList=$StoreModel->select();
		$this->assign('storeList',$storeList);
		$this->display();
	}
	public function delStore()
	{
		 $id = I('get.id', '', 'htmlspecialchars');
		 $StoreModel=M('store');
		 $rs=$StoreModel->where("id=$id")->delete();
		 if($rs!=false)
		 {
		 	$this->redirect('/Index/storeList');
		 }
	}
	public function editStore()
	{
		$id = I('get.id', '', 'htmlspecialchars');
		$StoreModel=M('store');
		$rs=$StoreModel->where("id=$id")->select();
		$rs=$rs[0];
		$this->assign('store',$rs);
		$this->display();
	}
	public function submitStore()
	{
		$id = $_POST['id'];
		echo $id;
		$StoreModel=M('store');
		$data['address']=$_POST['address'];
 		$data['name']=$_POST['name'];
 		$data['store_boss']=$_POST['store_boss'];
 		$data['phone']=$_POST['phone'];
 		$data['password']=$_POST['password'];
 		
 		if(empty($_POST['is_open'])){
 			$data['is_open']='0';
 		}else{
 			$data['is_open']=$_POST['is_open'];
 		}
		$rs=$StoreModel->where("id=$id")->save($data);
		$this->redirect('/Index/storeList');
	}
	public function addstore()
	{
 		$this->display();
	}
	public function submitNewStore()
	{
		$StoreModel=D('store');
		if (!$StoreModel->create()){
			exit($StoreModel->getError());
		}
		else
		{
			$StoreModel->add();
		  	 $this->redirect('/Index/storeList');
		}
	}
	public function bangStore()
	{
		$id = I('get.id', '', 'htmlspecialchars');
		$Wbar=M('wbar');
		$rs=$Wbar->where("id=$id")->select();
		$rs=$rs[0];
		$wbarStore=D('WbarStore');
		$where['in_wbar_store.wbar_id']=$id;
		$bangStoreList=$wbarStore->field('in_wbar_store.*,s.name')->
		join('left join in_store s on s.id=in_wbar_store.store_id')->where($where)->select();	
		$StoreModel=M('store');
		$storeList=$StoreModel->select();
		$this->assign('storeList',$storeList);
		$this->assign('wbar',$rs);
		$this->assign('bangStoreList',$bangStoreList);
		
		$this->display();
	}
	public function bangStoreByStoreIdAndWbarId()
	{
		$store_id = I('get.store_id', '', 'htmlspecialchars');
		$wbar_id = I('get.wbar_id', '', 'htmlspecialchars');
		$wbarStore=M('wbar_store');
		$where['store_id']=$store_id;
		$where['wbar_id']=$wbar_id;
		
		$rs=$wbarStore->where($where)->select();
		if($rs){
		$this->redirect("/Index/bangStore",array('id' => $wbar_id));
		}else{
			$wbarStore->data($where)->add();
		}
		$this->redirect("/Index/bangStore",array('id' => $wbar_id));
	}
	
	public function cancleStoreByStoreIdAndWbarId()
	{
		$store_id = I('get.store_id', '', 'htmlspecialchars');
		$wbar_id = I('get.wbar_id', '', 'htmlspecialchars');
		$wbarStore=M('wbar_store');
		$where['store_id']=$store_id;
		$where['wbar_id']=$wbar_id;
		$wbarStore->where($where)->delete();
		$this->redirect("/Index/bangStore",array('id' => $wbar_id));
	}
	
	public function tongji()
	{
		$this->display();
	}
    public function submitFrom()
    {
        $object['title']=array();
        $month= $_POST['month'];
        array_push($object['title'],$month."月网吧外卖订单");
        array_push($object['title'],$month."月网吧超市订单");
        $object['wbar']=array();
        $orders=M('orders');
        $where['create_time']=array('like', '%'.$month.'%');
        $list=$orders->Distinct(true)->field('wbar_id')->where($where)->select();
        foreach($list as $order)
        {
            array_push($object['wbar'],$order['wbar_id']);
        }
        $object['number']=array();
        $wbarModel=M('wbar');
        $object['wbar_name']=array();
        foreach($object['wbar'] as $wb)
        {
            $w['id']=$wb;
            $name=$wbarModel->where($w)->getField('name');
            array_push($object['wbar_name'],$name);
        }
        foreach($object['wbar'] as $wb)
        {
            $where['wbar_id']=$wb;
            $l=$orders->where($where)->select();
            array_push($object['number'],count($l));
        }
        $object['wbar_number']=array();
        $wbarOrderModel=M('wbar_order');
        foreach($object['wbar'] as $wb)
        {
            $where['wbar_id']=$wb;
            $l=$wbarOrderModel->where($where)->select();
            if(empty($l))
            {
                array_push($object['wbar_number'],0);
            }else{
                array_push($object['wbar_number'],count($l));
            }

        }




        $this->ajaxReturn($object);
    }
}
?>